<?php

    /**
    * @framework : My Framework
    *
    * @author: fatangel26@gmail.com - Nguyễn Bảo Trung.
    *
    */

	include "config/database.php";

	class my_model extends database
	{
		public $result;
		public $select;
		public $where;
		public $order;
		public $limit;

		public function __construct()
		{
			parent::__construct();
		}

		public function query($sql)
		{
			$this->result = mysql_query($sql);
		}

		public function num_rows()
		{
			if($this->result)
			{
				$row = mysql_num_rows($this->result);
			}
			else
			{
				$row = 0;
			}

			return $row;
		}

		public function fetch_one()
		{
			if($this->result)
			{
				$row = mysql_fetch_assoc($this->result);
			}

			return $row;
		}

		public function fetch_all()
		{
			if($this->result)
			{
				while($row = mysql_fetch_assoc($this->result))
				{
					$data[] = $row;
				}
			}

			return $data;
		}

		public function insert_id()
		{
			return mysql_insert_id();
		}

		public function affected_row()
		{
			return mysql_affected_rows();
		}

		public function insert($table,$data)
		{
			$key 	= array_keys($data);
			$col 	= implode(",",$key);
			$value 	= array_values($data);
			foreach($value as $item)
			{
			    /*NEED*/
			    if(is_string($item))
                {
                	if($item !== 'NOW()')
                    	$arr[] = "'$item'";
                    else
                    	$arr[] = $item;
                }
                else
                {
                    $arr[] = $item;
                }
			}

			$val = implode(",",$arr);
			$sql = "INSERT INTO ".$table."(".$col.") VALUES (".$val.")";
			$this->query($sql);
		}

		public function update($table,$data)
		{
			foreach($data as $k=>$v)
			{
				$arr[] = "$k='$v'";
			}
			$str = implode(",",$arr);
			if($this->where != "")
			{
				$where = " WHERE ".$this->where;
			}

			$sql = "UPDATE ".$table." SET ".$str.$where;
			$this->query($sql);
		}

		public function delete($table)
		{
			if($this->where != "")
			{
				$where = " WHERE ".$this->where;
			}
			else
			{
				$where = "";
			}
			$sql = "DELETE FROM ".$table.$where;
			$this->query($sql);
		}

		public function select($str)
		{
			$this->select = $str;
		}

		public function where($where)
		{
			if(is_array($where))
			{
				foreach($where as $k=>$v)
				{
					$arr[] = "$k = '$v'";
				}
				$this->where = implode(" AND ",$arr);
			}
			else
			{
				$this->where = $where;
			}
		}

		public function order($field, $str)
		{
			$this->order = $field." ".$str;
		}

		public function limit($str)
		{
			$this->limit = $str;
		}

		public function get($table)
		{
			if($this->select != "")
			{
				$select = $this->select;
			}
			else
			{
				$select = "*";
			}
			if($this->where != "")
			{
				$where = " WHERE ".$this->where;
			}
			else
			{
				$where = "";
			}
			if($this->order != "")
			{
				$order = " ORDER BY ".$this->order;
			}
			else
			{
				$order = "";
			}
			if($this->limit != "")
			{
				$limit = " LIMIT ".$this->limit;
			}
			else
			{
				$limit = "";
			}

			$sql = "SELECT ".$select." FROM ".$table.$where.$order.$limit;
           	//print($sql);
			$this->query($sql);
		}
	}

	/* End of file my_model.php */
	/* Location: .//D/root/my_framework/core/my_model.php */